package com.starsoft.frame.fileimport.pojo;

import java.util.ArrayList;
import java.util.List;

import com.starsoft.frame.util.StringUtil;

public class Table {

	public enum TitleType {
		Null, OnlyName, OnlyCode, NameAndCode
	}

	private List<Column> columnList;
	private TitleType titleType;

	private int sheetIndex = 0;
	private int startCol = 0;
	private int endCol = -1;
	private int nameRow = -1;
	private int codeRow = -1;
	private int startRow = 0;
	private int endRow = -1;

	public Table() {

	}

	public Table(TitleType type, List<Column> columnList) {
		titleType = type;
		this.columnList = columnList;
		switch (type) {
		case Null:
			break;
		case OnlyName:
			this.setRows(0, -1, 1, -1);
			break;
		case OnlyCode:
			this.setRows(-1, 0, 1, -1);
			break;
		case NameAndCode:
			this.setRows(0, 1, 2, -1);
			this.columnList = columnList;
			break;
		default:
			break;
		}
	}

	public void setRows(int nameRow, int codeRow, int startRow, int endRow) {
		this.nameRow = nameRow;
		this.codeRow = codeRow;
		this.startRow = startRow;
		this.endRow = endRow;
	}

	public void createColumnList() {
		if (columnList == null) {
			int colNum = this.getColumnNum();
			columnList = new ArrayList<Column>(colNum);
			for (int i = 0; i < colNum; i++) {
				Column col = new Column(i);
				columnList.add(col);
			}
		}
	}

	public int getColumnNum() {
		if (columnList != null) {
			return columnList.size();
		} else {
			return endCol - startCol;
		}
	}
	
	public int getRowNum() {
		return endRow - startRow;
	}

	public Column getColumnByName(String name){
		if(StringUtil.isEmpty(name)){
			return null;
		}
		for(Column column:columnList){
			if(name.equals(column.getName())){
				return column;
			}
		}
		return null;
	}
	

	public Column getColumnByCode(String code) {
		if(StringUtil.isEmpty(code)){
			return null;
		}
		for(Column column:columnList){
			if(code.equals(column.getCode())){
				return column;
			}
		}
		return null;
	}
	
	public List<Column> getColumnList() {
		return columnList;
	}

	public void setColumnList(List<Column> columnList) {
		this.columnList = columnList;
	}

	public TitleType getTitleType() {
		return titleType;
	}

	public void setTitleType(TitleType titleType) {
		this.titleType = titleType;
	}

	public int getSheetIndex() {
		return sheetIndex;
	}

	public void setSheetIndex(int sheetIndex) {
		this.sheetIndex = sheetIndex;
	}

	public int getStartCol() {
		return startCol;
	}

	public void setStartCol(int startCol) {
		this.startCol = startCol;
	}

	public int getEndCol() {
		return endCol;
	}

	public void setEndCol(int endCol) {
		this.endCol = endCol;
	}

	public int getNameRow() {
		return nameRow;
	}

	public void setNameRow(int nameRow) {
		this.nameRow = nameRow;
	}

	public int getCodeRow() {
		return codeRow;
	}

	public void setCodeRow(int codeRow) {
		this.codeRow = codeRow;
	}

	public int getStartRow() {
		return startRow;
	}

	public void setStartRow(int startRow) {
		this.startRow = startRow;
	}

	public int getEndRow() {
		return endRow;
	}

	public void setEndRow(int endRow) {
		this.endRow = endRow;
	}
}
